package br.com.tiespec.ejb.Dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.DescricaoEstendida;
import br.com.tiespec.model.Util.DAOException;

@Stateless(mappedName = "DescricaoEstendidaDAO")
public class DescricaoEstendidaDAO extends DAO {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<DescricaoEstendida> listarDescricaoEstendida(int first, int pageSize, String sortField,
			TipoSortOrder sortOrder, Long idClientePortal, String codigo, String nome, String projeto) {
		List<DescricaoEstendida> aList = new ArrayList<DescricaoEstendida>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(DescricaoEstendida.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			aList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return aList;
	}

	public Integer countDescricaoEstendida(Long idClientePortal, String codigo, String nome, String projeto) {
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(DescricaoEstendida.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			
			criteria.setProjection(Projections.rowCount());
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	public DescricaoEstendida selecionar(Long id) {
		DescricaoEstendida model = new DescricaoEstendida();
		try {
			begin();
			model = (DescricaoEstendida) this.session.createCriteria(DescricaoEstendida.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return model;
	}

	public void remover(Long id) throws DAOException {
		delete(DescricaoEstendida.class, id);
	}
	

	public DescricaoEstendida selecionarCompleta(Long id) {
		DescricaoEstendida model = new DescricaoEstendida();
		try {
			begin();
			model = (DescricaoEstendida) this.session.createCriteria(DescricaoEstendida.class)
					.add(Restrictions.eq("id", id))
					.setFetchMode("dados", FetchMode.JOIN)
//					.setFetchMode("atores", FetchMode.JOIN)
					.setFetchMode("projeto", FetchMode.JOIN)
					.uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return model;
	}
	
	@SuppressWarnings("unchecked")
	public List<DescricaoEstendida> listarDescricaoEstendida(int first, int pageSize, String sortField,
			TipoSortOrder sortOrder, Long idProjeto) {
		List<DescricaoEstendida> deList = new ArrayList<DescricaoEstendida>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(DescricaoEstendida.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("projeto.id", idProjeto));
			
			criteria = montaPaginacaoSimples(criteria, first, pageSize, sortField, sortOrder);
			deList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return deList;
	}

	public Integer countDescricaoEstendida(Long idProjeto) {
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(DescricaoEstendida.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("projeto.id", idProjeto))
					.setProjection(Projections.rowCount());
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}

	@SuppressWarnings("unchecked")
	public List<DescricaoEstendida> listarDescricaoEstendida(Long idProjeto) {
		List<DescricaoEstendida> deList = new ArrayList<DescricaoEstendida>();
		try {
			begin();
			Criteria criteria = this.session.createCriteria(DescricaoEstendida.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
					.add(Restrictions.eq("projeto.id", idProjeto));
			deList = criteria.list();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return deList;
	}

}